﻿using BW.WMS.Domain.Entities;
using BW.WMS.Infrastructure.Data.Contexts;
using Dapper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static BW.WMS.Infrastructure.Data.Contexts.DBContext;
using MySql.Data.MySqlClient;

namespace BW.WMS.Infrastructure.Data.Repositories
{
    /// <summary>
    /// 仓储实现
    /// </summary>
    public class DataRepository<T>
    {
        private readonly DBContext db;

        public DataRepository(DBContext db)
        {
            this.db = db;
        }
        /// <summary>
        /// 获取
        /// </summary>
        /// <param name="id"></param>
        /// <param name="sql"></param>
        /// <returns></returns>
        public T Get(int id,string sql)
        {
            return db.GetConn().QueryFirstOrDefault<T>(sql, new { Id = id });
        }
        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public List<T> GetList(string sql)
        {
            return db.GetConn().Query<T>(sql).ToList();
        }
        public List<T> GetList(string sql, object parameters)
        {
            return db.GetConn().Query<T>(sql, parameters).ToList();
        }

        // 新增：支持参数化的Add方法（关键修改）
        public int Add(string sql, object parameters)
        {
            return db.GetConn().Execute(sql, parameters);
        }
        //public int Add(string sql)
        //{
        //    return db.GetConn().QuerySingle<int>(sql);
        //}

        public bool Update(string sql)
        {
            int result = db.GetConn().Execute(sql);
            return result > 0;
        }
        public bool Delete(string sql)
        {
            int result = db.GetConn().Execute(sql);
            return result > 0;
        }

    }
}
